home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg3.cab / asynchat.py < prev    next >
Text File  |  2005-11-19  |  11KB  |  295 lines

  1. # -*- Mode: Python; tab-width: 4 -*-
  2. #       Id: asynchat.py,v 2.26 2000/09/07 22:29:26 rushing Exp
  3. #       Author: Sam Rushing <rushing@nightmare.com>
  4.  
  5. # ======================================================================
  6. # Copyright 1996 by Sam Rushing
  7. #
  8. #                         All Rights Reserved
  9. #
  10. # Permission to use, copy, modify, and distribute this software and
  11. # its documentation for any purpose and without fee is hereby
  12. # granted, provided that the above copyright notice appear in all
  13. # copies and that both that copyright notice and this permission
  14. # notice appear in supporting documentation, and that the name of Sam
  15. # Rushing not be used in advertising or publicity pertaining to
  16. # distribution of the software without specific, written prior
  17. # permission.
  18. #
  19. # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  20. # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
  21. # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  22. # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  23. # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  24. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  25. # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. # ======================================================================
  27.  
  28. r"""A class supporting chat-style (command/response) protocols.
  29.  
  30. This class adds support for 'chat' style protocols - where one side
  31. sends a 'command', and the other sends a response (examples would be
  32. the common internet protocols - smtp, nntp, ftp, etc..).
  33.  
  34. The handle_read() method looks at the input stream for the current
  35. 'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n'
  36. for multi-line output), calling self.found_terminator() on its
  37. receipt.
  38.  
  39. for example:
  40. Say you build an async nntp client using this class.  At the start
  41. of the connection, you'll have self.terminator set to '\r\n', in
  42. order to process the single-line greeting.  Just before issuing a
  43. 'LIST' command you'll set it to '\r\n.\r\n'.  The output of the LIST
  44. command will be accumulated (using your own 'collect_incoming_data'
  45. method) up to the terminator, and then control will be returned to
  46. you - by calling your self.found_terminator() method.
  47. """
  48.  
  49. import socket
  50. import asyncore
  51.  
  52. class async_chat (asyncore.dispatcher):
  53.     """This is an abstract class.  You must derive from this class, and add
  54.     the two methods collect_incoming_data() and found_terminator()"""
  55.  
  56.     # these are overridable defaults
  57.  
  58.     ac_in_buffer_size       = 4096
  59.     ac_out_buffer_size      = 4096
  60.  
  61.     def __init__ (self, conn=None):
  62.         self.ac_in_buffer = ''
  63.         self.ac_out_buffer = ''
  64.         self.producer_fifo = fifo()
  65.         asyncore.dispatcher.__init__ (self, conn)
  66.  
  67.     def collect_incoming_data(self, data):
  68.         raise NotImplementedError, "must be implemented in subclass"
  69.  
  70.     def found_terminator(self):
  71.         raise NotImplementedError, "must be implemented in subclass"
  72.  
  73.     def set_terminator (self, term):
  74.         "Set the input delimiter.  Can be a fixed string of any length, an integer, or None"
  75.         self.terminator = term
  76.  
  77.     def get_terminator (self):
  78.         return self.terminator
  79.  
  80.     # grab some more data from the socket,
  81.     # throw it to the collector method,
  82.     # check for the terminator,
  83.     # if found, transition to the next state.
  84.  
  85.     def handle_read (self):
  86.  
  87.         try:
  88.             data = self.recv (self.ac_in_buffer_size)
  89.         except socket.error, why:
  90.             self.handle_error()
  91.             return
  92.  
  93.         self.ac_in_buffer = self.ac_in_buffer + data
  94.  
  95.         # Continue to search for self.terminator in self.ac_in_buffer,
  96.         # while calling self.collect_incoming_data.  The while loop
  97.         # is necessary because we might read several data+terminator
  98.         # combos with a single recv(1024).
  99.  
  100.         while self.ac_in_buffer:
  101.             lb = len(self.ac_in_buffer)
  102.             terminator = self.get_terminator()
  103.             if terminator is None or terminator == '':
  104.                 # no terminator, collect it all
  105.                 self.collect_incoming_data (self.ac_in_buffer)
  106.                 self.ac_in_buffer = ''
  107.             elif isinstance(terminator, int):
  108.                 # numeric terminator
  109.                 n = terminator
  110.                 if lb < n:
  111.                     self.collect_incoming_data (self.ac_in_buffer)
  112.                     self.ac_in_buffer = ''
  113.                     self.terminator = self.terminator - lb
  114.                 else:
  115.                     self.collect_incoming_data (self.ac_in_buffer[:n])
  116.                     self.ac_in_buffer = self.ac_in_buffer[n:]
  117.                     self.terminator = 0
  118.                     self.found_terminator()
  119.             else:
  120.                 # 3 cases:
  121.                 # 1) end of buffer matches terminator exactly:
  122.                 #    collect data, transition
  123.                 # 2) end of buffer matches some prefix:
  124.                 #    collect data to the prefix
  125.                 # 3) end of buffer does not match any prefix:
  126.                 #    collect data
  127.                 terminator_len = len(terminator)
  128.                 index = self.ac_in_buffer.find(terminator)
  129.                 if index != -1:
  130.                     # we found the terminator
  131.                     if index > 0:
  132.                         # don't bother reporting the empty string (source of subtle bugs)
  133.                         self.collect_incoming_data (self.ac_in_buffer[:index])
  134.                     self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:]
  135.                     # This does the Right Thing if the terminator is changed here.
  136.                     self.found_terminator()
  137.                 else:
  138.                     # check for a prefix of the terminator
  139.                     index = find_prefix_at_end (self.ac_in_buffer, terminator)
  140.                     if index:
  141.                         if index != lb:
  142.                             # we found a prefix, collect up to the prefix
  143.                             self.collect_incoming_data (self.ac_in_buffer[:-index])
  144.                             self.ac_in_buffer = self.ac_in_buffer[-index:]
  145.                         break
  146.                     else:
  147.                         # no prefix, collect it all
  148.                         self.collect_incoming_data (self.ac_in_buffer)
  149.                         self.ac_in_buffer = ''
  150.  
  151.     def handle_write (self):
  152.         self.initiate_send ()
  153.  
  154.     def handle_close (self):
  155.         self.close()
  156.  
  157.     def push (self, data):
  158.         self.producer_fifo.push (simple_producer (data))
  159.         self.initiate_send()
  160.  
  161.     def push_with_producer (self, producer):
  162.         self.producer_fifo.push (producer)
  163.         self.initiate_send()
  164.  
  165.     def readable (self):
  166.         "predicate for inclusion in the readable for select()"
  167.         return (len(self.ac_in_buffer) <= self.ac_in_buffer_size)
  168.  
  169.     def writable (self):
  170.         "predicate for inclusion in the writable for select()"
  171.         # return len(self.ac_out_buffer) or len(self.producer_fifo) or (not self.connected)
  172.         # this is about twice as fast, though not as clear.
  173.         return not (
  174.                 (self.ac_out_buffer == '') and
  175.                 self.producer_fifo.is_empty() and
  176.                 self.connected
  177.                 )
  178.  
  179.     def close_when_done (self):
  180.         "automatically close this channel once the outgoing queue is empty"
  181.         self.producer_fifo.push (None)
  182.  
  183.     # refill the outgoing buffer by calling the more() method
  184.     # of the first producer in the queue
  185.     def refill_buffer (self):
  186.         while 1:
  187.             if len(self.producer_fifo):
  188.                 p = self.producer_fifo.first()
  189.                 # a 'None' in the producer fifo is a sentinel,
  190.                 # telling us to close the channel.
  191.                 if p is None:
  192.                     if not self.ac_out_buffer:
  193.                         self.producer_fifo.pop()
  194.                         self.close()
  195.                     return
  196.                 elif isinstance(p, str):
  197.                     self.producer_fifo.pop()
  198.                     self.ac_out_buffer = self.ac_out_buffer + p
  199.                     return
  200.                 data = p.more()
  201.                 if data:
  202.                     self.ac_out_buffer = self.ac_out_buffer + data
  203.                     return
  204.                 else:
  205.                     self.producer_fifo.pop()
  206.             else:
  207.                 return
  208.  
  209.     def initiate_send (self):
  210.         obs = self.ac_out_buffer_size
  211.         # try to refill the buffer
  212.         if (len (self.ac_out_buffer) < obs):
  213.             self.refill_buffer()
  214.  
  215.         if self.ac_out_buffer and self.connected:
  216.             # try to send the buffer
  217.             try:
  218.                 num_sent = self.send (self.ac_out_buffer[:obs])
  219.                 if num_sent:
  220.                     self.ac_out_buffer = self.ac_out_buffer[num_sent:]
  221.  
  222.             except socket.error, why:
  223.                 self.handle_error()
  224.                 return
  225.  
  226.     def discard_buffers (self):
  227.         # Emergencies only!
  228.         self.ac_in_buffer = ''
  229.         self.ac_out_buffer = ''
  230.         while self.producer_fifo:
  231.             self.producer_fifo.pop()
  232.  
  233.  
  234. class simple_producer:
  235.  
  236.     def __init__ (self, data, buffer_size=512):
  237.         self.data = data
  238.         self.buffer_size = buffer_size
  239.  
  240.     def more (self):
  241.         if len (self.data) > self.buffer_size:
  242.             result = self.data[:self.buffer_size]
  243.             self.data = self.data[self.buffer_size:]
  244.             return result
  245.         else:
  246.             result = self.data
  247.             self.data = ''
  248.             return result
  249.  
  250. class fifo:
  251.     def __init__ (self, list=None):
  252.         if not list:
  253.             self.list = []
  254.         else:
  255.             self.list = list
  256.  
  257.     def __len__ (self):
  258.         return len(self.list)
  259.  
  260.     def is_empty (self):
  261.         return self.list == []
  262.  
  263.     def first (self):
  264.         return self.list[0]
  265.  
  266.     def push (self, data):
  267.         self.list.append (data)
  268.  
  269.     def pop (self):
  270.         if self.list:
  271.             return (1, self.list.pop(0))
  272.         else:
  273.             return (0, None)
  274.  
  275. # Given 'haystack', see if any prefix of 'needle' is at its end.  This
  276. # assumes an exact match has already been checked.  Return the number of
  277. # characters matched.
  278. # for example:
  279. # f_p_a_e ("qwerty\r", "\r\n") => 1
  280. # f_p_a_e ("qwertydkjf", "\r\n") => 0
  281. # f_p_a_e ("qwerty\r\n", "\r\n") => <undefined>
  282.  
  283. # this could maybe be made faster with a computed regex?
  284. # [answer: no; circa Python-2.0, Jan 2001]
  285. # new python:   28961/s
  286. # old python:   18307/s
  287. # re:        12820/s
  288. # regex:     14035/s
  289.  
  290. def find_prefix_at_end (haystack, needle):
  291.     l = len(needle) - 1
  292.     while l and not haystack.endswith(needle[:l]):
  293.         l -= 1
  294.     return l
  295.